package com.gemserk.infraestructure.lwjglmavenizer

import java.io.File;
import java.util.jar.Attributes 
import java.util.jar.JarFile;
import java.util.jar.Manifest 
import java.util.zip.ZipEntry 
import java.util.zip.ZipFile 
import java.util.zip.ZipOutputStream 

class ZipUtils {
	
	public void unzip(File zipPath, File targetDir) throws IOException
	{
		new File("hola").
		
		println "Unpacking $zipPath"
		ZipFile zip = new ZipFile(zipPath);

		Enumeration<ZipEntry> entries = zip.entries();
		while (entries.hasMoreElements())
		{
			ZipEntry file = entries.nextElement();

			File f = new File(targetDir, file.getName());

			File parentFile = f.getParentFile();
			parentFile.mkdirs();
			
			if (file.isDirectory())
			{ // if its a directory, create it
				f.mkdir();
				continue;
			}

			InputStream is = null;

			try
			{
				is = zip.getInputStream(file); // get the input stream
				f << is
			}
			finally
			{
				if (is != null)
					is.close();
			}

		}

	}
	
	
	public ZipOutputStream createZipFile(def path){
		try {
			ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(path));
			return zos
		}
		catch(Exception e) {
			e.printStackTrace()
		}
	}
	
	public void addManifest(ZipOutputStream zos){
		Manifest manifest = new Manifest()
		manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION,"1.0")
		zos.putNextEntry(new ZipEntry(JarFile.MANIFEST_NAME))
		manifest.write(zos)
	}
	
	public void zipFromDir(String dir2zip, ZipOutputStream zos){
		zipDir(dir2zip,dir2zip,zos)
	}
	
	//here is the code for the method
	public void zipDir(String dir2zip, String dir2zipBase, ZipOutputStream zos)
	{
		try {
			//create a new File object based on the directory we have to zip File
			def zipDir = new File(dir2zip);
			//get a listing of the directory content
			String[] dirList = zipDir.list();
			
			//loop through dirList, and zip the files
			for(int i=0; i<dirList.length; i++)
			{
				File f = new File(zipDir, dirList[i]);
				if(f.isDirectory()) {
					//if the File object is a directory, call this
					//function again to add its content recursively
					String filePath = f.getAbsolutePath();
					this.zipDir(filePath, dir2zipBase, zos);
					//loop again
					continue;
				}
				zipFile(f,dir2zipBase,zos)
			}
		}
		catch(Exception e) {
			e.printStackTrace()
		}
	}
	
	public void zipFile(File file, String relativeToDir, ZipOutputStream zos){
		//if we reached here, the File object f was not	a directory
		//create a FileInputStream on top of f
		FileInputStream fis = new FileInputStream(file);
		//create a new zip entry
		ZipEntry anEntry = new ZipEntry(file.getAbsolutePath().substring(relativeToDir.length()+1));
		//place the zip entry in the ZipOutputStream object
		zos.putNextEntry(anEntry);
		//now write the content of the file to the ZipOutputStream
		byte[] readBuffer = new byte[2156];
		int bytesIn = 0;
		while((bytesIn = fis.read(readBuffer)) != -1)
		{
			zos.write(readBuffer, 0, bytesIn);
		}
		//close the Stream
		fis.close();
	}
	
	public static void main(String[] args) {
		ZipUtils zu = new ZipUtils()
		def zos = zu.createZipFile("/tmp/sources.zip")
		zu.zipFromDir("/tmp/lwjgl-source-2.5/src/generated",zos)
		zu.zipFromDir("/tmp/lwjgl-source-2.5/src/java",zos)
		zos.close()
	}
}
	